package br.com.lucasddaniel.merchanmobile.activities;

import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import br.com.lucasddaniel.merchanmobile.R;
import br.com.lucasddaniel.merchanmobile.bean.TuristaBean;
import br.com.lucasddaniel.merchanmobile.dao.ExcessaoBanco;
import br.com.lucasddaniel.merchanmobile.Controller;

public class TuristasActivity extends Activity implements OnClickListener {

	private TextView m_textviewNumRegistro;
	private TextView m_textviewVooIn;
	private TextView m_textviewPousada;
	private TextView m_textviewNome;
	private TextView m_textviewQuantidade;
	private TextView m_textviewVooOut;
	private TextView m_textviewObs;
	private TextView m_textviewMotorista;
	private TextView m_textviewVeiculo;
	private TuristaBean turistaBean;

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_turista );

		if( getIntent().getExtras() != null && getIntent().getExtras().getInt( "idTurista" ) > 0 ) {
			Integer id = getIntent().getExtras().getInt( "idTurista" );
			try {
				turistaBean = Controller.getInstance().buscarTurista( new TuristaBean( id ), getApplicationContext() );
			} catch( ExcessaoBanco e ) {
				e.printStackTrace();
			}
		}

		( ( Button ) findViewById( R.id_activity_turistas.button_concluir ) ).setOnClickListener( this );
		initComponents();
		if( turistaBean != null ) {
			iniciaCampos();
		}
	}

	private void iniciaCampos() {
		m_textviewMotorista.setText( turistaBean.getMotorista() );
		m_textviewNome.setText( turistaBean.getNome() );
		m_textviewNumRegistro.setText( turistaBean.getNumRegistro() );
		m_textviewObs.setText( turistaBean.getObservacao() );
		m_textviewPousada.setText( turistaBean.getPousada() );
		m_textviewQuantidade.setText( turistaBean.getQuantidade() + "" );
		m_textviewVooIn.setText( turistaBean.getVooIn() + "" );
		m_textviewVooOut.setText( turistaBean.getVooOut() + "" );
	}

	@Override
	public void onBackPressed() {

		super.onBackPressed();

	}

	public void onClick( View v ) {
		switch( v.getId() ) {
			case R.id_activity_turistas.button_concluir:
				if( turistaBean == null ) {
					turistaBean = new TuristaBean();
				}
				if( m_textviewNome.getText() != null ) {
					turistaBean.setNome( m_textviewNome.getText().toString() );
				}
				if( m_textviewPousada.getText() != null ) {
					turistaBean.setPousada( m_textviewPousada.getText().toString() );
				}
				if( m_textviewQuantidade.getText() != null && m_textviewQuantidade.getText().toString().length() > 0 ) {
					Integer i = Integer.valueOf( m_textviewQuantidade.getText().toString() );
					turistaBean.setQuantidade( i );
				} else {
					turistaBean.setQuantidade( 0 );
				}
				if( m_textviewObs.getText() != null ) {
					turistaBean.setObservacao( m_textviewObs.getText().toString() );
				}

				if( turistaBean.getId() != null ) {
					editTurista();
				} else {
					turistaBean.setHoraChegada( new Date().getTime() );
					insertTurista();
				}
				this.finish();
				break;

		}
	}

	private void insertTurista() {
		try {
			Controller.getInstance().insereTurista( turistaBean, getApplicationContext() );
		} catch( ExcessaoBanco e ) {
			e.printStackTrace();
			Toast.makeText( this, "Problema no banco de dados. Por favor tente novamente", Toast.LENGTH_LONG ).show();
		}
	}

	private void editTurista() {

		try {
			Controller.getInstance().atualizaTurista( turistaBean, getApplicationContext() );
			Toast.makeText( this, "Dados salvos com sucesso.", Toast.LENGTH_LONG ).show();
		} catch( ExcessaoBanco e ) {
			e.printStackTrace();
			Toast.makeText( this, "Problemas na edição de dados. Por favor tente novamente.", Toast.LENGTH_LONG ).show();
		}
	}

	private void initComponents() {
		/*
		 * Nome pousada quantidade obs
		 */

		m_textviewVooOut = ( TextView ) findViewById( R.id_activity_turista.txtview_vooout );
		m_textviewVooIn = ( TextView ) findViewById( R.id_activity_turista.txtview_vooin );
		m_textviewQuantidade = ( TextView ) findViewById( R.id_activity_turista.txtview_quantidade );
		m_textviewPousada = ( TextView ) findViewById( R.id_activity_turista.txtview_pousada );
		m_textviewObs = ( TextView ) findViewById( R.id_activity_turista.txtview_observacao );
		m_textviewNumRegistro = ( TextView ) findViewById( R.id_activity_turista.txtview_numregistro );
		m_textviewNome = ( TextView ) findViewById( R.id_activity_turista.txtview_nome );
		m_textviewMotorista = ( TextView ) findViewById( R.id_activity_turista.txtview_motorista );
		m_textviewVeiculo = ( TextView ) findViewById( R.id_activity_turista.txtview_veiculo );

		Intent i = getIntent();
		boolean itensShowed = i.getBooleanExtra( "fromMenu", false );
		if( itensShowed ) {
			( ( LinearLayout ) findViewById( R.id_activity_turista.ll_vooout ) ).setVisibility( View.GONE );
			( ( LinearLayout ) findViewById( R.id_activity_turista.ll_vooin ) ).setVisibility( View.GONE );
			( ( LinearLayout ) findViewById( R.id_activity_turista.ll_motorista ) ).setVisibility( View.GONE );
			( ( LinearLayout ) findViewById( R.id_activity_turista.ll_numregistro ) ).setVisibility( View.GONE );
		}

	}
}
